#ifndef A9BF0322_18D4_4DAD_92FB_951DB0439C07
#define A9BF0322_18D4_4DAD_92FB_951DB0439C07

#include<iostream>
#include<sys/epoll.h>
#include"Socket.hpp"
#include"Log.hpp"
#include"Epoller.hpp"
#include<memory>


uint32_t EVENT_IN = (EPOLLIN);
uint32_t EVENT_OUT = (EPOLLOUT);


class Epollserver : nocopy
{

    static const int num = 64;

public:
    Epollserver(uint16_t port) 
    : port_(port),
    listensocket_ptr(new Socket()),
    epoller_ptr(new Epoller())
    {}

    ~Epollserver()
    {}

    void Init()
    {
        listensocket_ptr->InitSocket();
        listensocket_ptr->Bind(port_);
        listensocket_ptr->Listen();

        lg(Info, "create listen socket success: %d\n", listensocket_ptr->Fd());
    }


    void Start()
    {
        //将listensock添加到epoll中 -> listensock和他关心的事件， 添加到内核epoll模型中rb_tree中。
        struct epoll_event revs[num];
        for(;;)
        {
            int n = epoller_ptr->EpollErWait(revs, num);

            if(n > 0)
            {
                //有事件就绪

            }
            else if (n == 0)
            {
                lg(Info, "time out ...");
            }
            else
            {
                lg(Error, "epoll wait error");
            }
        }

    }



    ~Epollserver()
    {
        listensocket_ptr->Close();
    }


private:
    shared_ptr<Socket> listensocket_ptr;
    shared_ptr<Epoller> epoller_ptr;

    uint16_t port_;

};




#endif /* A9BF0322_18D4_4DAD_92FB_951DB0439C07 */
